package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.dto.emp.EmployeeTagChangeDTO;
import com.pactera.madp.cp.api.dto.emp.EmployeeTagCreateDTO;
import com.pactera.madp.cp.api.entity.BaseEntity;
import com.pactera.madp.cp.api.entity.employeeWechat.EmployeeTagEntity;
import com.pactera.madp.cp.api.entity.employeeWechat.EmployeeTagPivotEntity;
import com.pactera.madp.cp.api.vo.EmployeeTagVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.EmployeeTagMapper;
import com.pactera.madp.cp.service.EmployeeTagPivotService;
import com.pactera.madp.cp.service.EmployeeTagService;
import com.pactera.madp.cp.service.IContactService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (通讯录)标签(CpWorkEmployeeTag)表服务实现类
 *
 * @author makejava
 * @since 2021-06-29 10:31:44
 */
@Service
@Data
@Slf4j
public class EmployeeTagServiceImpl extends ServiceImpl<EmployeeTagMapper, EmployeeTagEntity> implements EmployeeTagService {

    private final EmployeeTagPivotService employeeTagPivotService;
    @Resource
    private  IContactService contactService;

    @Override
    public Map<Object, Object> insert(EmployeeTagCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        EmployeeTagEntity employeeTagEntity = new EmployeeTagEntity();
        employeeTagEntity.setTagName(dto.getTagName());
        employeeTagEntity.setWxExternalUserid(dto.getWxExternalUserid());
        employeeTagEntity.setCorpId(corpId);
        employeeTagEntity.setWxUserId(dto.getWxUserId());
        EmployeeTagPivotEntity employeeTagPivotEntity = new EmployeeTagPivotEntity();
        employeeTagPivotEntity.setWxUserId(dto.getWxUserId());
        employeeTagPivotEntity.setWxExternalUserid(dto.getWxExternalUserid());
        employeeTagPivotEntity.setCorpId(corpId);
        employeeTagPivotService.save(employeeTagPivotEntity);
        save(employeeTagEntity);
        HashMap<Object, Object> hashMap = new HashMap<>();
        EmployeeTagEntity one = getOne(Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getCorpId,corpId).orderByDesc(BaseEntity::getCreateTime).last("limit 1"));
        hashMap.put("id",one.getId());
        hashMap.put("tagName",one.getTagName());
        return hashMap;
    }

    @Override
    public List<EmployeeTagVO> list(String wxExternalUserid, String wxUserId) {
        return baseMapper.labelByUser(wxExternalUserid,wxUserId,CorpContextHolder.getCorpId());
    }

    @Override
    public boolean delete(Integer id) {
        EmployeeTagEntity one = getOne(Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId, id));
        EmployeeTagPivotEntity one1 = employeeTagPivotService.getOne(Wrappers.<EmployeeTagPivotEntity>lambdaQuery().eq(EmployeeTagPivotEntity::getId, id).eq(EmployeeTagPivotEntity::getWxExternalUserid, one.getWxExternalUserid()));
        contactService.userTagDeleteContact(one.getWxExternalUserid(),one1.getWxUserId(),one.getTagName());
        return remove(Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId,id));
    }

    @Override
    public void changeState(EmployeeTagChangeDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        List<EmployeeTagVO> employeeTagVOS = baseMapper.labelByUser(dto.getWxExternalUserid(),dto.getWxUserId(),CorpContextHolder.getCorpId());
        ArrayList<Integer> idsAll = new ArrayList<>();
        employeeTagVOS.forEach(entity->{
            Integer id = entity.getId();
            idsAll.add(id);
        }
        );
        ArrayList<Integer> newIds = new ArrayList<>();
        for(int i=0;i<=dto.getIds().length-1;i++){
            Integer id = dto.getIds()[i];
            EmployeeTagEntity employeeTagEntity = new EmployeeTagEntity();
            employeeTagEntity.setId(id);
            employeeTagEntity.setTagState(1);
            employeeTagEntity.setCorpId(corpId);
            update(employeeTagEntity,Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId,id));
            newIds.add(id);
            EmployeeTagEntity one = getOne(Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId, id));
            EmployeeTagPivotEntity one1 = employeeTagPivotService.getOne(Wrappers.<EmployeeTagPivotEntity>lambdaQuery().eq(EmployeeTagPivotEntity::getId, id).eq(EmployeeTagPivotEntity::getWxExternalUserid, one.getWxExternalUserid()).eq(EmployeeTagPivotEntity::getCorpId,CorpContextHolder.getCorpId()));
            contactService.userTagSaveContact(one.getWxExternalUserid(),one1.getWxUserId(),one.getTagName());
        }
         idsAll.removeAll(newIds);
         idsAll.forEach(id->{
             EmployeeTagEntity employeeTagEntity = new EmployeeTagEntity();
             employeeTagEntity.setId(id);
             employeeTagEntity.setTagState(2);
             update(employeeTagEntity,Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId,id));
         });
    }

    @Override
    public List<EmployeeTagVO> stateList(String wxExternalUserid, String wxUserId) {
        return baseMapper.changeLabelByUser(wxExternalUserid,wxUserId,CorpContextHolder.getCorpId());
    }

    @Override
    public boolean changeStateById(Integer id) {
        EmployeeTagEntity employeeTagEntity = new EmployeeTagEntity();
        employeeTagEntity.setTagState(2);
        employeeTagEntity.setId(id);
        return update(employeeTagEntity,Wrappers.<EmployeeTagEntity>lambdaQuery().eq(EmployeeTagEntity::getId,id));
    }

    @Override
    public Integer isDistinct(String wxExternalUserid, String wxUserId, String tagName) {
        log.info("wxExternalUserid"+wxExternalUserid);
        log.info("wxUserId"+wxUserId);
        log.info("tagName"+tagName);
        return baseMapper.isDistinct(wxExternalUserid, wxUserId, tagName,CorpContextHolder.getCorpId());
    }

    @Override
    public List<EmployeeTagVO> getAllList() {
       return   list(Wrappers.<EmployeeTagEntity>lambdaQuery().eq(BaseEntity::getDelFlag, 0).eq(EmployeeTagEntity::getTagState, 1)).stream().map(entity->{
            EmployeeTagVO employeeTagVO = new EmployeeTagVO();
            BeanUtil.copyProperties(entity,employeeTagVO);
            return employeeTagVO;
        }).collect(Collectors.toList());
    }
}
